using System.Reflection;
using NUnit.Framework;
using ContentRenderingApi;
using ContentRenderingApiSystemTest.Helper;
using ContentRenderingApi.Path;

namespace ContentRenderingApiSystemTest.TestFixture
{
    [TestFixture]
    public class RenderCacheTestFixture
    {
        [Test]
        public void ValueCacheOnTest()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                "ContentRenderingApiSystemTest.TestFixture.Input",
                "ValueCache");

            template.AddNamedParameter("test", "aaa");

            CounterHelper childCounterHelper = new CounterHelper();

            template.TransformationConfiguration.AddObserverFunction(new FullPath("test"), (nodeExtender) =>
            {
                childCounterHelper.Increment();
            });

            CounterHelper valueCounterHelper = new CounterHelper();

            template.TransformationConfiguration.AddGlobalFormatter<string>(value => 
            { 
                valueCounterHelper.Increment(); 
                return value.ToString(); 
            });

            Assert.AreEqual(
                ResourceHelper.FetchString("ContentRenderingApiSystemTest.TestFixture.Output.ValueCache.txt"),
                template.Render());

            Assert.AreEqual(valueCounterHelper.Counter, 1);
            Assert.AreEqual(childCounterHelper.Counter, 1);
        }

        [Test]
        public void ValueCacheOffTest()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                "ContentRenderingApiSystemTest.TestFixture.Input",
                "ValueCache");

            template.IsCaching = false;

            template.AddNamedParameter("test", "aaa");

            CounterHelper childCounterHelper = new CounterHelper();

            template.TransformationConfiguration.AddObserverFunction(new FullPath("test"), (nodeExtender) =>
            {
                childCounterHelper.Increment();
            });

            CounterHelper valueCounterHelper = new CounterHelper();

            template.TransformationConfiguration.AddGlobalFormatter<string>(value =>
            {
                valueCounterHelper.Increment();
                return value.ToString();
            });

            Assert.AreEqual(
                ResourceHelper.FetchString("ContentRenderingApiSystemTest.TestFixture.Output.ValueCache.txt"),
                template.Render());

            Assert.AreEqual(valueCounterHelper.Counter, 3);
            Assert.AreEqual(childCounterHelper.Counter, 3);
        }
    }
}
